package cn.springspace.kotlinapp.service

import cn.springspace.kotlinapp.model.Door
import cn.springspace.kotlinapp.repository.DoorRepository
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.BodyInserters.fromObject
import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.function.server.ServerResponse
import reactor.core.publisher.Mono

@Component
class RestDoorHandler(val doorRepository: DoorRepository) {
    fun saveDoor(request: ServerRequest): Mono<ServerResponse> = request.bodyToMono(Door::class.java)
            .flatMap { it -> Mono.just(doorRepository.save(it)) }
            .flatMap { it ->
                ServerResponse.ok().body(fromObject(it))
            }
}